package com.xb.fileoperate.controller;

import com.xb.fileoperate.model.User;
import com.xb.fileoperate.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Description: 前端控制器<br>
 *
 * @author: XB
 * @date: 2021/8/16 14:06
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/getUser")
    public User getUser(){
        return userService.getById(1);
    }


    @GetMapping("/findAllUser")
    public List<User> findAllUser(){
        return userService.findAllUser();
    }

    @GetMapping("/addUser")
    public List<User> addUser(){
        List<User> oldList = userService.findAllUser();
        User user = new User();
        int index = CollectionUtils.isEmpty(oldList) ? 1 : oldList.size() + 1;
        user.setLoginName("user" + index);
        user.setName("用户" + index);
        user.setPasswd("123456");
        user.setRoleId(2L);
        userService.save(user);
        return userService.findAllUser();
    }
}
